//
//  LinnerSetViewController.m
//  LINNER
//
//  Created by wei wang on 2017/4/18.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "LinnerSetViewController.h"
#import "LinnerLightSetViewController.h"
#import "BurnViewController.h"
#import "LanguageViewController.h"
#import "AboutlinnerViewController.h"
@interface LinnerSetViewController ()<UITableViewDelegate,UITableViewDataSource>

/** titleArray **/
@property(nonatomic,strong) NSArray *titleArray;
/** tableview **/
@property(nonatomic,strong) UITableView *tableView;

/** lightArray **/
@property(nonatomic,strong) NSArray *lightArray;

/** laguage **/
@property(nonatomic,strong) NSArray *laguage;

@end

@implementation LinnerSetViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self getHeadset];
}

- (void)getHeadset{
    
    /*获取耳机效果灯状态*/
    NSString *sendata = @"aa0002ae";
    const char *buf = [sendata UTF8String];
    [self sendata:buf];
    [self setNavigationStyle];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        /*获得耳机降噪／监听状态*/
        NSString *sendata = @"aa000292";
        const char *buf = [sendata UTF8String];
        [self sendata:buf];
        
    });

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self masLayoutSubview];
    self.titleArray = @[MyNSLocalizedString(@"装饰灯设置"),MyNSLocalizedString(@"煲机模式"),MyNSLocalizedString(@"语音提示"),MyNSLocalizedString(@"固件升级"),MyNSLocalizedString(@"使用说明")];
    self.lightArray = @[MyNSLocalizedString(@"默认"),MyNSLocalizedString(@"常亮"),MyNSLocalizedString(@"呼吸灯"),MyNSLocalizedString(@"关闭")];
    self.laguage = @[@"中文",@"English"];
    //灯
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableView) name:GetHEADSETLIGHT object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableView) name:GetHEADSETLAGUAGE object:nil];
}


- (void)updateTableView{
    
    [self.tableView reloadData];
}

- (void)setNavigationStyle{
    
    [self setNavigationBarTitle:MyNSLocalizedString(@"设置")];
    [self setLeftButtonImage:[UIImage imageNamed:@"tittle_back_nor"]];
    [self.navigationController.navigationBar makeTransparent_nornav];
    
}

- (void)sendata:(const char*)buf{
    
    NSMutableData *data = [NSMutableData data];
    if (buf)
    {
        uint32_t len = (uint32_t)strlen(buf);
        
        char singleNumberString[3] = {'\0', '\0', '\0'};
        uint32_t singleNumber = 0;
        for(uint32_t i = 0 ; i < len; i+=2)
        {
            if ( ((i+1) < len) && isxdigit(buf[i]) && (isxdigit(buf[i+1])) )
            {
                singleNumberString[0] = buf[i];
                singleNumberString[1] = buf[i + 1];
                sscanf(singleNumberString, "%x", &singleNumber);
                uint8_t tmp = (uint8_t)(singleNumber & 0x000000FF);
                [data appendBytes:(void *)(&tmp) length:1];
            }
            else
            {
                break;
            }
        }
        NSLog(@"综合发送%@",data);
        
        [[EADSessionController sharedController] writeData:data];
    }
    
}


#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titleArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.textLabel.textColor = [UIColor colorWithHexString:@"#56504f"];
        [cell.textLabel setFont:[UIFont systemFontOfSize:15.f]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [cell.detailTextLabel setFont:[UIFont systemFontOfSize:15.f]];

    }
    
    cell.textLabel.text = MyNSLocalizedString(self.titleArray[indexPath.row]);
    
    if (indexPath.row == 0) {
        
        if ([AppDelegate globalDelegate].GetheadsetLight == -1) {
            
        }else{
            
            cell.detailTextLabel.text = MyNSLocalizedString(self.lightArray[[AppDelegate globalDelegate].GetheadsetLight]);
        }
    }
    if (indexPath.row == 2){
        
        if ([AppDelegate globalDelegate].GetheadsetLaugage == -1) {
            
        }else{
            
            cell.detailTextLabel.text = MyNSLocalizedString(self.laguage[[AppDelegate globalDelegate].GetheadsetLaugage]);
        }

    }
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 56;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}
#pragma mark - --------------------------UITableView delegate--------------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:
        {
            LinnerLightSetViewController *vc = [LinnerLightSetViewController new];
            [self.navigationController pushViewController:vc animated:YES];

        }
            break;
        case 1:{
            
            BurnViewController *vc = [BurnViewController new];
            vc.indexTag = 1;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:{
            
            LanguageViewController *vc = [LanguageViewController new];
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            
            break;
            case 3:
        {
            [SVProgressHUD showInfoWithStatus:MyNSLocalizedString(@"最新版本")];

        }
            break;
            case 4:
        {
            AboutlinnerViewController *vc = [AboutlinnerViewController new];
            [self.navigationController pushViewController:vc animated:YES];

        }
            
            
            break;
            default:
            break;
    }
    
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        
    }
    
    return _tableView;
    
}

- (void)masLayoutSubview{
    
    
    self.tableView.frame = CGRectMake(0, 74, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
